package com.byzk.p2p.admin.borrow.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.borrow.dto.Guarantee;
import com.byzk.p2p.admin.borrow.service.GuaranteeService;

/**
 * 担保方Controller
 */
@Controller
@RequestMapping("/admin/guarantee")
public class GuaranteeController extends AdminBaseController {
	@Autowired
	private GuaranteeService guaranteeService;

	/**
	 * 分页查询担保方列表
	 */
	@RequestMapping("/list")
	public String list(Model model, 
			@RequestParam(value = "index", required = false, defaultValue = "1") int index,
			@RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
			@RequestParam(value = "name", required = false) String name) {
		Map<String, Object> param = new HashMap<String, Object>();
		try {
			if (StringUtils.isNotBlank(name)) {
				name = URLDecoder.decode(name, "UTF-8");
				param.put("name", name);
				model.addAttribute("name", name);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		PageInfo<Guarantee> pageInfo = guaranteeService.getPageInfo(param, index, limit);
		model.addAttribute("pageInfo", pageInfo);
		return super.ftl(Thread.currentThread().getStackTrace());
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public void delete(@RequestParam int id) {
		try {
			boolean flag = guaranteeService.delete(id);
			if (flag) {
				super.printJSON(new JsonMsg(1, "操作成功！"));
			} else {
				super.printJSON(new JsonMsg(2, "操作失败！"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 设置担保方信息
	 */
	@RequestMapping("/setting")
	public String setting(Model model,
			@RequestParam(value="id", required=false,defaultValue="0") int id){
		Guarantee guarantee = guaranteeService.getById(id);
		model.addAttribute("item", guarantee);
		return super.ftl(Thread.currentThread().getStackTrace());
	}
	
	/**
	 * 保存担保方信息
	 */
	@RequestMapping("/save")
	public void save(HttpServletRequest request, Guarantee guarantee){
		try {
			boolean flag = guaranteeService.saveOrUpdate(guarantee);
			if(flag) {
				super.printJSON(new JsonMsg(-1, "保存成功！"));
			}else {
				super.printJSON(new JsonMsg(2, "保存失败！"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
